package de.bib.dashboard;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Class untuk membuat log ke file.
 * 
 * @author 'EkoWahyudi'
 * 
 */
public final class LogPrinter {

	/**
	 * Format waktu.
	 */
	private SimpleDateFormat format = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss.SSS");

	/**
	 * Writer ke file.
	 */
	private BufferedWriter writer;

	/**
	 * Instance log printer.
	 */
	private static LogPrinter logPrinter = null;

	/**
	 * Nama proses.
	 */
	private String processName = null;

	/**
	 * private constructor.
	 */
	private LogPrinter() {
	}

	/**
	 * Private konstruktor untuk tempatkan file log.
	 * 
	 * @param processName
	 *            nama proses
	 * 
	 * @param folderLog
	 *            folder log file
	 * @param node
	 *            nama node
	 * @param timeTag
	 *            tag waktu
	 * @throws IOException
	 *             error I/O
	 */
	private LogPrinter(String processName, String folderLog, String node,
			String timeTag) throws IOException {
		this.processName = processName;
		File folder = new File(folderLog, node);
		if (!folder.exists()) {
			folder.mkdirs();
		}

		File file = new File(folder, timeTag + ".log");
		writer = new BufferedWriter(new FileWriter(file, true));
	}

	/**
	 * Buat instan dari log printer.
	 * 
	 * @param processName
	 *            nama proses
	 * @param folderLog
	 *            folder file log
	 * @param node
	 *            nama node
	 * @param timeTag
	 *            tag waktu
	 * @return log printer
	 */
	public static LogPrinter createInstance(String processName,
			String folderLog, String node, String timeTag) {
		try {
			logPrinter = new LogPrinter(processName, folderLog, node, timeTag);
		} catch (IOException e) {
			System.out.println("Fail create log printer, because "
					+ e.getMessage());
		}
		return logPrinter;
	}

	/**
	 * Mendapatkan instan dari class log printer.
	 * 
	 * @return log printer
	 */
	public static LogPrinter getInstance() {
		if (logPrinter == null) {
			logPrinter = new LogPrinter();
		}
		return logPrinter;
	}

	/**
	 * Tampilkan log start.
	 */
	public void startLog() {
		info("Start");
	}

	/**
	 * Print pesan kesalahan ke file log.
	 * 
	 * @param message
	 *            pesan kesalahan.
	 * @throws IOException
	 */
	public void error(String message) {
		writeln(format.format(new Date()) + " ERROR " + processName + " "
				+ message);
	}

	/**
	 * Print kondisi warning ke file log.
	 * 
	 * @param message
	 *            pesan.
	 * @throws IOException
	 */
	public void warning(String message) {
		writeln(format.format(new Date()) + " WARNING " + processName + " "
				+ message);
	}

	/**
	 * Print pesan ke file log.
	 * 
	 * @param message
	 *            pesan
	 * @throws IOException
	 */
	public void info(String message) {
		writeln(format.format(new Date()) + " INFO " + processName + " "
				+ message);
	}

	/**
	 * Tulis ke file.
	 * 
	 * @param message
	 *            pesan
	 */
	public void writeln(String message) {
		try {
			String line = message + "\n";
			if (writer != null) {
				writer.write(line);
				writer.flush();
			}
			System.out.print(line);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Stop logging.
	 * 
	 * @param show
	 *            tampilkan finish
	 * @throws IOException
	 *             error IO
	 */
	public void finish(boolean show) throws IOException {
		if (show) {
			info("Finish");
		}

		if (writer != null) {
			writer.close();
		}
	}

	/**
	 * Stop logging.
	 * 
	 * @throws IOException
	 *             error IO
	 */
	public void finish() throws IOException {
		finish(true);
	}
}
